/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
* License for more details.
*
* $Id: ircbuffer.h 581 2009-05-19 16:40:38Z jpnurmi $
*/

#ifndef IRC_BUFFER_H
#define IRC_BUFFER_H

#include <ircglobal.h>
#include <QByteArray>
#include <QBuffer>

namespace Irc
{
    class Session;
    class BufferPrivate;

    class IRC_EXPORT Buffer : public QBuffer
    {
        Q_OBJECT
        Q_PROPERTY(QByteArray name READ name)
        Q_PROPERTY(QByteArray encoding READ encoding WRITE setEncoding)

    public:
        ~Buffer();

        QByteArray name() const;
        Session* session() const;

        QByteArray encoding() const;
        void setEncoding(const QByteArray& encoding);

        // reimplemented from QIODevice
        virtual bool waitForReadyRead(int msecs);

    public Q_SLOTS:
        void raw(const QByteArray& message);
        void message(const QByteArray& message);
        void notice(const QByteArray& notice);
        void ctcpAction(const QByteArray& action);
        void ctcpRequest(const QByteArray& request);
        void ctcpReply(const QByteArray& reply);

    Q_SIGNALS:
        void ctcpActionReceived(const QByteArray& prefix, const QByteArray& action);
        void ctcpReplyReceived(const QByteArray& prefix, const QByteArray& reply);
        void ctcpRequestReceived(const QByteArray& prefix, const QByteArray& request);
        void messageReceived(const QByteArray& prefix, const QByteArray& message);
        void noticeReceived(const QByteArray& prefix, const QByteArray& notice);
        void numericMessageReceived(const QByteArray& prefix, uint code, const QByteArray& message);

    protected:
        Buffer(const QByteArray& name, Session* session, BufferPrivate& dd);
        qint64 writeData(const char* data, qint64 maxSize);
        BufferPrivate* const d_ptr;

    private:
        Buffer(const QByteArray& name, Session* session);
        Q_DECLARE_PRIVATE(Buffer)
        Q_DISABLE_COPY(Buffer)
        friend class Session;
        friend class SessionPrivate;

        Q_PRIVATE_SLOT(d_func(), void _q_bytesWritten(qint64))
    };
}

#endif // IRC_BUFFER_H
